#coding=utf-8
import smtplib  
import email.MIMEMultipart

import email.MIMEText# import MIMEText  
import email.MIMEBase# import MIMEBase  
import os.path  
import mimetypes  
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr
import smtplib
import time
From = "autotest@tcl.com" 

mail_1 = "w.p@tcl.com"
mail_2 = "wuzhxiong@tcl.com"
mail_3 = "youjp@tcl.com"
mail_4 = "yaohj@tcl.com"
mail_5 = "fuhongbiao@tcl.com"
mail_6 = "huangpg@tcl.com"
mail_7 = "yangyouguang@huawei.com"
mail_8 = "tianchunjiang@huawei.com"
mail_9 = "stefan.yangfan@huawei.com"
mail_10 = 'yaozhenhui@huawei.com'
mail_11 ='sunwenli@tcl.com'
Tototal_mail=[mail_1,mail_2,mail_3,mail_4,mail_5,mail_6,mail_7,mail_8,mail_9,mail_10,mail_11]


file_name = r"E:\eclipse\huawei_rader\all_mobile\huawei_honor6_result.html"   #

print '邮箱开始发送'

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr(( \
        Header(name, 'utf-8').encode(), \
        addr.encode('utf-8') if isinstance(addr, unicode) else addr))


server = smtplib.SMTP("mail.tcl.com",25)   
server.ehlo()
server.starttls()
time.sleep(1)
server.set_debuglevel(1)
server.login("autotest@tcl.com","NEW2016#")
main_msg = email.MIMEMultipart.MIMEMultipart()  
text_msg = email.MIMEText.MIMEText("各位，你好 :     \n\
          请查收华华为小雷达自动化测试报告（如果测试结果明显不对，需要考虑测试过程中小雷达是否死机或者蓝牙无法连接，导致后面结果全部fail）、\n\
          此邮件为自动邮件，请勿回复\n\
          备注：由于测试的是手机，测试完一个手机需要手动更换一个手机，一个手机输出一份测试报告，还望谅解\n\
          附件建议“另存为”到本地电脑，然后打开 ,否则部分测试结果可能看不到\n\
   adb log和手机截图由于容量太大，存放在服务器：'\\10.84.100.35\\auto_test\\report_save\\huawei_radar\\huawei_honor6'，请知悉\n\n\
BR DQA王鹏        ",_charset="utf-8")  


main_msg.attach(text_msg)  

data = open(file_name, 'rb')  
ctype,encoding = mimetypes.guess_type(file_name)  
if ctype is None or encoding is not None:  
    ctype = 'application/octet-stream'  
maintype,subtype = ctype.split('/',1)  
file_msg = email.MIMEBase.MIMEBase(maintype, subtype)  
file_msg.set_payload(data.read())  
data.close( )  
email.Encoders.encode_base64(file_msg)
basename = os.path.basename(file_name)  
file_msg.add_header('Content-Disposition','attachment', filename = basename)
main_msg.attach(file_msg)  
 
main_msg['From'] = _format_addr(u'DQA自动化测试 <%s>' %From)

#main_msg['Tototal_mail'] = Tototal_mail 
main_msg['Subject'] = "华为空气闹钟项目自动化测试报告 --华为荣耀6手机 "  
main_msg['Date'] = email.Utils.formatdate( )  

fullText = main_msg.as_string( )  

try:  
    for val in Tototal_mail:
        main_msg['val'] = val
        server.sendmail(From, val, fullText)  
finally:  
    server.quit()  
print '邮箱发送结束'